﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.入库管理
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ScrapController : BaseController
    {
        private readonly IScrapService _service;
        public ScrapController(IWebHostEnvironment environment, IScrapService _service) : base(environment)
        {
            this._service = _service;
        }


        /// <summary>
        /// 增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddScrap(ScrapOrderDetail order)
        {
            string message = "";
            bool IsOK = _service.AddScrap(order, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败" + message, "500"));
        }


        /// <summary>
        /// 删
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelScrap(ScrapOrderDetail order)
        {
            bool IsOK = _service.DelScrap(order);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }


        /// <summary>
        /// 改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdScrap(ScrapOrderDetail order)
        {
            bool IsOK = _service.UpdScrap(order);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }


        /// <summary>
        /// 查全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllScrapOrders(int pageIndex, int pageSize, string? orderSn)
        {
            var list = _service.GetAllScrapOrders(pageIndex, pageSize, orderSn, out int totalCount);
            return Ok(BaseResult.Result(list, "查询成功", "200",totalCount));
        }


        /// <summary>
        /// 根据id查
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetScrapOrder(long id)
        {
            var model = _service.GetScrapOrder(id);
            if (model != null)
            {
                return Ok(BaseResult.Result(model, "查询成功", "200"));
            }
            return Ok(BaseResult.Result(false, "查询失败", "500"));
        }
    }
}
